using Microsoft.AspNetCore.Mvc;
using ZR.Model.ProdManage.Dto;
using ZR.Model.ProdManage;
using ZR.Service.ProdManage.IProdManageService;
using ZR.Admin.WebApi.Filters;
using MiniExcelLibs;

//创建时间：2025-04-28
namespace ZR.Admin.WebApi.Controllers.ProdManage
{
    /// <summary>
    /// bom信息
    /// </summary>
    [Verify]
    [Route("ProdManage/ProdBom")]
    public class ProdBomController : BaseController
    {
        /// <summary>
        /// bom信息接口
        /// </summary>
        private readonly IProdBomService _ProdBomService;

        public ProdBomController(IProdBomService ProdBomService)
        {
            _ProdBomService = ProdBomService;
        }

        /// <summary>
        /// 查询bom信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("pageList")]
        [ActionPermissionFilter(Permission = "prod:bom:list")]
        public IActionResult PageProdBom([FromQuery] ProdBomQueryDto parm)
        {
            var response = _ProdBomService.GetPageList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询bom信息详情
        /// </summary>
        /// <param name="BomId"></param>
        /// <returns></returns>
        [HttpGet("{BomId}")]
        [ActionPermissionFilter(Permission = "prod:bom:query")]
        public IActionResult GetProdBom(string BomId)
        {
            var response = _ProdBomService.GetInfo(BomId);

            var info = response.Adapt<ProdBomDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加bom信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "prod:bom:add")]
        [Log(Title = "bom信息", BusinessType = BusinessType.INSERT)]
        public IActionResult AddProdBom([FromBody] ProdBomDto parm)
        {
            var modal = parm.Adapt<ProdBom>().ToCreate(HttpContext);

            var response = _ProdBomService.AddProdBom(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新bom信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "prod:bom:edit")]
        [Log(Title = "bom信息", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateProdBom([FromBody] ProdBomDto parm)
        {
            var modal = parm.Adapt<ProdBom>().ToUpdate(HttpContext);
            var response = _ProdBomService.UpdateProdBom(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除bom信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "prod:bom:delete")]
        [Log(Title = "bom信息", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteProdBom([FromBody] string[] ids)
        {
            return ToResponse(_ProdBomService.Delete(ids));
        }

        /// <summary>
        /// 导出bom信息
        /// </summary>
        /// <returns></returns>
        [Log(Title = "bom信息", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "prod:bom:export")]
        public IActionResult Export([FromQuery] ProdBomQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _ProdBomService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "bom信息", "bom信息");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "bom信息导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false)]
        [ActionPermissionFilter(Permission = "prod:bom:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        {
            List<ProdBomDto> list = new();
            using (var stream = formFile.OpenReadStream())
            {
                list = stream.Query<ProdBomDto>(startCell: "A1").ToList();
                list.ForEach(it =>
                {
                    it.BomId = Guid.NewGuid().ToString().ToUpper();
                    it.CreateBy = HttpContext.GetName();
                    it.CreateTime = DateTime.Now;
                });
            }

            return SUCCESS(_ProdBomService.ImportProdBom(list.Adapt<List<ProdBom>>()));
        }

        /// <summary>
        /// bom信息导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("importTemplate")]
        [Log(Title = "bom信息模板", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult ImportTemplateExcel()
        {
            var result = DownloadImportTemplate(new List<ProdBomDto>() { }, "ProdBom");
            return ExportExcel(result.Item2, result.Item1);

            //(string, string) result = DownloadImportTemplate("KD-BOM导入模板");
            //return ExportExcel(result.Item2, result.Item1);
        }

    }
}